//
//  XJLoginViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJLoginViewController.h"

#import "XJRegistViewController.h"
#import "XJForgetPasswdViewController.h"
#import "NSString+LSMD5.h"

#import "LMJUMengHelper.h"

@interface XJLoginViewController ()<XJLoginDelegate>

@end

@implementation XJLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"登录";
    //self.navButtonRight.hidden = YES;
//    self.navButtonLeft.hidden = NO;

    self.xjLoginView.delegate = self;
 
//    if (self.presentationController) {
        [self setupNav];
//    }
}
- (void)setupNav {
    
    // 左侧返回按钮:建议创建button用遍历构造器方法
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    backBtn.frame = CGRectMake(10, 0, 10, 20);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:(UIControlStateNormal)];
    [backBtn addTarget:self action:@selector(dismissAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
#ifdef __IPHONE_11_0
    if(@available(iOS 11.0,*)){
//        [backBtn.widthAnchor constraintEqualToConstant:10].active = YES;
//        [backBtn.heightAnchor constraintEqualToConstant:20].active = YES;
    }
#endif
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)dismissAction:(UIButton *)btn {
    XJLog(@"pod掉");
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (XJLoginView *)xjLoginView{
    
    if (!_xjLoginView) {
        XJLoginView * xjLoginView = [[XJLoginView alloc]initWithFrame:CGRectMake(0, XJTopNavH, KWidth, KHeight)];
        self.xjLoginView = xjLoginView;
        [self.view addSubview:self.xjLoginView];

    }
    return _xjLoginView;
    
}

#pragma mark XJLoginDelegate

- (void)loginWithUserName:(NSString *)userName code:(NSString *)codeString{
    NSString *md5Str = [NSString md5:codeString];
    
    // 坑:区分大小写 服务器要的是小写，不然提示密码不正确。
    md5Str = md5Str.lowercaseString;
    XJLog(@"%@ == md5:%@",userName,md5Str);
    
    [XJHttpTool requestLoginWithPhoneNum:userName password:md5Str successHandler:^(id responseObjc) {

        NSNumber *codeNumber = responseObjc[@"code"];
        if ([codeNumber isEqualToNumber:@0]) {
            // 字典转模型
            NSDictionary *dic = responseObjc[@"data"][@"customer"];
            [self useNSUserDefalultSavaUserInfo:dic]; // 使用NSUserDefault保存用户信息
            
            [XJUserModel xj_saveLoginStatus]; // 标记登录成功
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
        
    } failureHandler:^(NSError *error) {
        NSLog(@"失败:%@",error);
        NSString *errMsg = [NSString stringWithFormat:@"%@",[error localizedDescription]];
        [JRToast showWithText:errMsg];
    }];
}

- (void)useNSUserDefalultSavaUserInfo:(NSDictionary *)dic {
    
    // 本地保存用户信息
    [[NSUserDefaults standardUserDefaults] setValue:dic[@"avatar"] forKey:kAvatar]; //1.
    [[NSUserDefaults standardUserDefaults] setValue:dic[@"nickname"] forKey:kNickname];//2.
    [[NSUserDefaults standardUserDefaults] setValue:dic[@"phone"] forKey:kPhone];//3.
    [[NSUserDefaults standardUserDefaults] setValue:dic[@"uuid"] forKey:kUUID];//4.
    
    NSString *customer_id = [NSString stringWithFormat:@"%@",dic[@"customer_id"]];
    [[NSUserDefaults standardUserDefaults] setValue:customer_id forKey:kCustomerId]; //5. customer_id 有用
    
    NSString *balance = [NSString stringWithFormat:@"%@",dic[@"balance"]];
    [[NSUserDefaults standardUserDefaults] setValue:balance forKey:kBalance];//6.
    
    NSNumber *sex = dic[@"sex"]; //7.
    if ([sex isEqualToNumber:@1]) {
        [[NSUserDefaults standardUserDefaults] setValue:@"男" forKey:kSex];
    }else {
        [[NSUserDefaults standardUserDefaults] setValue:@"女" forKey:kSex];
    }
    
    // 是否设置了支付密码 //8.
    NSNumber *isPayPwdNumber = dic[@"is_pay_password"]; // 服务器返回的应该是NSNumber 类型的
    if ([isPayPwdNumber isEqualToNumber:@0]) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kISSettingPayPassword];
    }else {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kISSettingPayPassword];
    }
    
    // 是否实名认证过 //9.
    NSNumber *isIdentifierNumber = dic[@"is_identity"];
    if ([isIdentifierNumber isEqualToNumber:@1]) { //审核中
        [[NSUserDefaults standardUserDefaults] setValue:@"审核中" forKey:kISIedntifier];
        
    }else if([isPayPwdNumber isEqualToNumber:@2]){ // 已认证(审核通过)
        [[NSUserDefaults standardUserDefaults] setValue:@"已认证" forKey:kISIedntifier];

    }else { //3 未认证
        [[NSUserDefaults standardUserDefaults] setValue:@"未认证" forKey:kISIedntifier];
    }
}




- (void)forgetUserPasswordAction{
    XJForgetPasswdViewController * xjfpVC = [[XJForgetPasswdViewController alloc]init];
    [self.navigationController pushViewController:xjfpVC animated:YES];
    
}

- (void)registAction{
    XJRegistViewController * xjRegistVC = [[XJRegistViewController alloc]init];
    [self.navigationController pushViewController:xjRegistVC animated:YES];
    
//    [XJHttpTool requestShouCangSuccessHandler:^(id responseObjc) {
//        
//    } failureHandler:^(NSError *error) {
//        
//    }];
    
}

#pragma mark - 第三方登录
- (void)loginByWeiXin{
    XJLog(@"微信登录");
    
    [LMJUMengHelper getUserInfoForPlatform:UMSocialPlatformType_WechatSession completion:^(UMSocialUserInfoResponse *result, NSError *error) {
        
        NSLog(@"%@", result);
    }];
}

- (void)loginByQQ{
    XJLog(@"QQ登录");
    
    [LMJUMengHelper getUserInfoForPlatform:(UMSocialPlatformType_QQ) completion:^(UMSocialUserInfoResponse *result, NSError *error) {
        NSLog(@"%@--%@",result,error);
    }];
    
}

- (void)loginByWeiBo{
       XJLog(@"微博登录");
    [LMJUMengHelper getUserInfoForPlatform:UMSocialPlatformType_Sina completion:^(UMSocialUserInfoResponse *result, NSError *error) {
        
        NSLog(@"%@", result);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s--%d",__FUNCTION__,__LINE__);
    [self dismissViewControllerAnimated:YES completion:nil];
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    //[self.navigationController setNavigationBarHidden:NO animated:YES];
//   // self.tabBarController.tabBar.hidden=YES;
//}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc {
    NSLog(@"挂挂！！！");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
